﻿using King.Box.Utils.Global;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace King.Box.Service.Impl
{
    public class SafetyService : ISafetyService
    {
        private readonly IBasicService _basicService;

        public SafetyService(IBasicService basicService)
        {
            _basicService = basicService;
        }

        //记录账号安全信息
        private static Dictionary<int, SafetyUser> _safetyData = new Dictionary<int, SafetyUser>();

        /// <summary>
        /// 登录日志
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="ip"></param>
        /// <param name="isFail"></param>
        public void SafetyLog(int userId,bool isCompelte)
        {
            SafetyUser currentSafaty = _safetyData.FirstOrDefault(x => x.Key == userId).Value;

            //检查时候初始化 这里不会为Null
            if (currentSafaty == null)
                return;
            
            currentSafaty.UpdateTime = DateTime.Now;

            if (!isCompelte)
                currentSafaty.ErrCount += 1;

            _safetyData[userId] = currentSafaty;
        }

       /// <summary>
       /// 检查异常
       /// </summary>
       /// <param name="userId"></param>
       /// <param name="ipAddress"></param>
       /// <returns></returns>
        public Tuple<bool, string> CheckSafety(int userId)
        {
            if (_safetyData.ContainsKey(userId))
            {
                var safetyData = _safetyData[userId];
                if (safetyData.ErrCount >= _basicService.GetDictionaryValue<int>(GlobalConst.Dictionary_Safety_LimitCount))
                {
                    if (safetyData.LockTime == null)
                    {
                        safetyData.LockTime = DateTime.Now;
                        safetyData.UpdateTime = DateTime.Now;
                    }

                    _safetyData[userId] = safetyData;
                }

                if (safetyData.LockTime != null)
                {
                    if (safetyData.LockTime.Value.AddMinutes(_basicService.GetDictionaryValue<int>(GlobalConst.Dictionary_Safety_LockMinute)) > DateTime.Now)
                        return Tuple.Create<bool, string>(false, ErrorDic[-1]);
                    else
                    {
                        safetyData.ErrCount = 0;
                        safetyData.LockTime = null;
                        safetyData.UpdateTime = DateTime.Now;

                        _safetyData[userId] = safetyData;
                    }
                }
            }
            else
            {
                var safatyUser = new SafetyUser()
                {
                    UpdateTime = DateTime.Now,
                    AddTime = DateTime.Now,
                    LockTime = null,
                    UserId = userId,
                    ErrCount = 0,
                };
                _safetyData.Add(userId, safatyUser);
            }
            return Tuple.Create<bool, string>(true, string.Empty);
        }

        private static Dictionary<int, string> ErrorDic = new Dictionary<int, string>()
        {
             {-1,"账号已被禁止登录稍后再试" }
        };
    }


    public class SafetyUser
    {
        /// <summary>
        /// 用户Id
        /// </summary>
        public int UserId { get; set; }
        /// <summary>
        /// 错误次数
        /// </summary>
        public int ErrCount { get; set; }
        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime UpdateTime { get; set; }
        /// <summary>
        /// 新增时间
        /// </summary>
        public DateTime AddTime { get; set; }
        /// <summary>
        /// 锁定时间
        /// </summary>
        public DateTime? LockTime { get; set; }
    }
}
